<h1>Digitalus Tool</h1>
<form action="/digDeveloper/tool/content-type" method="post" id="full_form">
     <fieldset class="ui-widget-content ui-corner-all">
        <legend class="ui-state-default">Select a Module</legend>
        <dl>
            <dt class="select_module"><label>Select an Existing Module or Create a New One</label></dt>
            <dd class="select_module">
                <?php
                    $modules = Dig_Service_Module::listAll();
                    $options[] = 'Select a module';
                    foreach (array_keys($modules) as $module) {
                        $options[$module] = $module;
                    }
                    $options['create_new'] = '+ Create a new Module';
                    echo $this->formSelect('mod', null, null, $options);
                ?>
            </dd>
            <dt class="create_module"><label>Enter a unique name for your module</label></dt>
            <dd class="create_module"><?= $this->formText('new_module')?></dd>
            <dt class="create_module"></dt>
            <dd class="create_module">
                <?php echo $this->formSubmit('create_new_module', 'Create Module') . ' ' .
                        $this->formSubmit('cancel_create_module', 'Cancel');?>
            </dd>
        </dl>
    </fieldset>

    <fieldset class="ui-widget-content ui-corner-all">
        <legend class="ui-state-default">Define Content Type</legend>
        <dl>
            <dt><label>Enter the name for your content type</label></dt>
            <dd><?= $this->formText('name');?></dd>
            <dt><label>Define the content type properties (INI format)</label></dt>
            <dd><?= $this->formTextarea('props', null, array('class' => 'large'));?>
                <p class="toolbar"><a href="#" id="add_property">Add Property</a></p>
            </dd>
            <dt></dt>
            <dd><?= $this->formSubmit('create_content_type', 'Create Content Type');?></dd>
        </dl>
    </fieldset>
</form>


<?php $this->jQuery()->onLoadCaptureStart(); ?>
$('#mod').change(function() {
    if($(this).val() == 'create_new') {
        $.createModuleForm.show();
    };
});
$('#create_new_module').click(function(){
    $.createModuleForm.create();
    return false;
});
$('#cancel_create_module').click(function(){
    $.createModuleForm.reset();
    $.createModuleForm.hide();
    return false;
});
$('#add_property').click(function(){
    var property = prompt('Property key:', '');
    if(! property) {
        return false;
    } else {
        var defaultValue = prompt('Enter the default value for ' + property + ':', '');
        $('#props').val( $('#props').val() + '\nitem.property.' + property + ' = "' + defaultValue + '"');
    }
    return false;
});
$.createModuleForm = {
    show: function() {
        $('.select_module').hide();
        $('.create_module').show();
    },
    hide: function() {
        $('.select_module').show();
        $('.create_module').hide();
    },
    reset: function() {
        $('#new_module').val('');
    },
    create: function() {
        var module = this.val();
        $.post('/digDeveloper/tool/create-module', {new_module: this.val()}, function(response) {
            if(response == true) {
                $('#mod').append($("<option></option>").attr("value",module).text(module)).val(module);
                $.createModuleForm.hide();
            } else {
            alert
                $.dig.ui.message.add('there was an error adding your module', 'critical');
            }
        }, 'json');
    },
    val: function() {
        return $('#new_module').val();
    }
}

$.createModuleForm.hide();
<?php $this->jQuery()->onLoadCaptureEnd(); ?>